﻿package game.exchangeddata 
{
	import flash.net.NetConnection;
	/**
	 * ...
	 * @author Doan Nguyen Thuy Nha Truc (Bamboo)	
	 */
	public class ChannelInfo
	{
		
		public var listChannel:Array;
		public var listTimeChannel:Array;
		public var listBwChannel:Array;
		public var lastTimeConnect:uint;
		public var lastTimeConnectSuccess:uint;
		public var countCheckFms:uint = 0;
		
		private static const fms:String = "fms";
		private static const id:String = "id";
		private static const time:String = "time";
		private static const bw:String = "bw";
		
		public function ChannelInfo() 
		{
			
		}
		//A#rtmp://113.21.208.5:80/live@B#rtmp://203.211.147.50:37562/live@C#rtmp://122.0.17.85/live@D#rtmp://205.177.27.7/live@E#rtmp://121.11.76.230/live
		public function init(str:String):void {			
			listChannel = new Array();
			listTimeChannel = new Array();
			listBwChannel = new Array();
			var tempA:Array = str.split("@");
			var len:int = tempA.length;
			
			for (var i:int = 0; i < len; i++) {
				var tempC:String = tempA[i];
				var tempB:Array = tempC.split("#");
				var obj:Object = new Object();
				var str:String = tempB[0];
				str = str.substr(1, str.length);
				//trace("str:" + str)
				obj[ChannelInfo.id] = str;
				obj[ChannelInfo.fms] = tempB[1];
				listChannel.push(obj);
			}
			//trace("aaaa:"+listChannel.length);
		}
		
		public function getTimeCurrent():int {
			
			var timeCurrent:Date = new Date();
			var temp:uint = timeCurrent.getMinutes() * 60 * 1000 + timeCurrent.getSeconds() * 1000 + timeCurrent.getMilliseconds();
			return temp;
		}
		
		public function getTimeSuccess():void {			
			lastTimeConnectSuccess = getTimeCurrent();
			var temp:uint = lastTimeConnectSuccess - lastTimeConnect;
			var obj:Object = new Object();
			obj[ChannelInfo.id] = listChannel[countCheckFms][ChannelInfo.id];
			obj[ChannelInfo.fms] = listChannel[countCheckFms][ChannelInfo.fms];
			obj[ChannelInfo.time] = temp;
			//trace("obj.time" + countCheckFms +":" + temp);
			listTimeChannel.push(obj);
			listTimeChannel.sortOn("time", Array.NUMERIC);
			/*for (var i:int = 0; i < listTimeChannel.length; i++)
			{
				trace("i:" + i + "id:" + listTimeChannel[i][ChannelInfo.id] + ":" + listTimeChannel[i][ChannelInfo.fms] +":"+ listTimeChannel[i].time)
			}*/
			countCheckFms++;
			
		}
		
		public function getBwSuccess(bw:String):void {
			var obj:Object = new Object();
			obj[ChannelInfo.id] = listTimeChannel[countCheckFms][ChannelInfo.id];
			obj[ChannelInfo.fms] = listTimeChannel[countCheckFms][ChannelInfo.fms];
			obj[ChannelInfo.bw] = int(bw);
			listBwChannel.push(obj);
			listBwChannel.sortOn("bw", Array.NUMERIC);
			/*for (var i:int = 0; i < listBwChannel.length; i++)
			{
				trace("i:" + i + "id:" + listBwChannel[i][ChannelInfo.id] + ":" + listBwChannel[i][ChannelInfo.fms] +":"+ listBwChannel[i].bw)
			}*/
			countCheckFms++;
		}
		public function randomArray(data:Array):Array {
			
			//trace("randomArray");
			var tempArray:Array =  new Array();
			tempArray = data;
			
			/*for (var j:int = 0; j < tempArray.length; j++)
			{
				trace("randomArray:" + j + "id:" + tempArray[j][ChannelInfo.id] + ":" + tempArray[j][ChannelInfo.fms]);
			}*/
			
			var len:int = tempArray.length;
			if (len > 1) {
				
				for (var i:int  = 0; i < len; i++) 
				{
					var tmp:Object = tempArray[i];
					var num:uint = Math.random()*tempArray.length;
					while (num == i) {
						num = Math.random()*tempArray.length;
					}
					tempArray[i] = tempArray[num];
					tempArray[num] = tmp;
				}
			}
			
			/*for (j = 0; j < tempArray.length; j++)
			{
				trace("randomArray sau:" + j + "id:" + tempArray[j][ChannelInfo.id] + ":" + tempArray[j][ChannelInfo.fms]);
			}*/
			
			return tempArray;
		}
			
			
	}

}